<?php

class Costumers extends CI_Controller {

    function __construct() {
        parent::__construct();
        if ($this->session->userdata('logged_in')) {
            $session_data = $this->session->userdata('logged_in');
            $data['username'] = $session_data['username'];
        } else {
            redirect('login/costumer', 'refresh');
        }
        $this->load->model('costumer');
        $this->load->model('berita');
        $this->load->model('booking');
        $this->load->model('email');
        $this->load->model('rute');
//        $this->load->model('jadwal');
        $this->load->model('airline');
        $this->load->model('deposit');
        $this->load->model('departure');
        $this->load->helper('deposit_helper');
        $this->load->helper('user_helper');
        $this->load->helper('document');
        $this->load->model('ym');
    }

    function index() {
        $id = $this->id();
        $data['id'] = $id;
        $data['expire'] = $this->costumer->get_expire($id);
        $data['yms'] = $this->ym->admin();
        

        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/index';
//        $this->load->view('template/costumer', $data);
        $this->load->view('template/home1', $data);
    }

    function deposit() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $id_book = $this->uri->segment(3);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();
        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['rows'] = $this->costumer->index($id);
        $data['bookings'] = $this->costumer->conissued($id_book, $id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/deposit';
        $this->load->view('template/home1', $data);
    }

    function info() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['i'] = 1;
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['rows'] = $this->costumer->index($id);
        $data['content'] = 'costumers/info';
        $this->load->view('template/home1', $data);
    }

    function halaman2() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/pencarian';
        $this->load->view('template/home1', $data);
    }

    function halaman3() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/halaman3';
        $this->load->view('template/home1', $data);
    }

    function halaman4() {
        $id = $this->id();
        $data['id'] = $id;
        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/halaman4';
        $this->load->view('template/home1', $data);
    }

    function halaman5() {
        $id = $this->id();
        $data['id'] = $id;
        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['content'] = 'costumers/halaman5';
        $this->load->view('template/home1', $data);
    }

    function show_news() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['news'] = $this->berita->index();
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();

        $id_news = $this->uri->segment(3);
        $data['id'] = $id;
        $data['news'] = $this->berita->show($id_news);
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'page/show';
        $this->load->view('template/home1', $data);
    }

    function status_book() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['i'] = 1;

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();

        $config = array();
        $config["base_url"] = base_url() . "costumers/status_book";
        $config["total_rows"] = $this->costumer->book_hitung($id);
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["rows"] = $this->costumer->fetch_book($config["per_page"], $page, $id);
        $data["links"] = $this->pagination->create_links();
        $data['id'] = $id;
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'page/status_book';
        $this->load->view('template/home1', $data);
    }

    function status_issued() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['i'] = 1;
        $data['yms'] = $this->ym->admin();

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();

        $config = array();
        $config["base_url"] = base_url() . "costumers/status_issued";
        $config["total_rows"] = $this->costumer->issued_hitung($id);
        $config["per_page"] = 10;
        $config["uri_segment"] = 3;

        $this->pagination->initialize($config);

        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data["rows"] = $this->costumer->fetch_issued($config["per_page"], $page, $id);
        $data["links"] = $this->pagination->create_links();
        $data['id'] = $id;
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'page/status_issued';
        $this->load->view('template/home1', $data);
    }

    function conissued() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $id_book = $this->uri->segment('3');

        $data['rows'] = $this->costumer->conissued($id_book, $id);
        $data['content'] = 'page/issued';
        $this->load->view('template/home1', $data);
    }

    function show_book() {
        $arr = $id_book = $this->uri->segment(3);
        $arr = explode('.', $arr);

        if (count($arr) == 1) {
            $id_book = $this->uri->segment(3);
        } else {
            $id_book = $arr[0];
        }
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['id'] = $id;
        $data['rows'] = $this->costumer->show_book($id_book, $id);
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'page/show_book';
        $this->load->view('template/home1', $data);
    }

    function test() {
        $id_book = 73;
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['id'] = $id;
        $data['rows'] = $this->costumer->show_book($id_book, $id);
        $data['deposits'] = $this->deposit->index($id);
        $email = $this->email->get_email();
        $email_user = $this->costumer->get_email($id);

      
//
        $data['id'] = $id;
        $data['rows'] = $this->costumer->show_book($id_book, $id);
        $data['deposits'] = $this->deposit->index($id);
//        $this->load->view('costumers/email_book', $data);
//        $config['mailtype'] = 'html';
//        $this->email->initialize($config);
//        
//        $message = $this->load->view('costumers/email_test', $data, TRUE);
        $this->load->view('costumers/email_test', $data);
//        echo"a";
//        
//        exit;
////        
//
//        $this->load->library('email');
//        $this->email->set_mailtype("html");
//        $this->email->from('stasiuntiketonline.com');
//
//        //email
//        $list = array('chaerul.developer@gmail.com', 'chaerul_u@rocketmail.com');
//        $this->email->to($list);
//        $this->email->cc('');
//        $this->email->subject('Issued tiket');
//        $this->email->message($message);
//        $this->email->send();
    }

    function cancel_booking() {
        $arr = $this->uri->segment(3);
        $arr = explode('.', $arr);
        $id = $arr[0];
        $code = $arr[1];
//        var_dump($id);
        $this->booking->cancel_booking($id, $code);
    }

    function edit() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['i'] = 1;
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['id'] = $id;
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['content'] = 'costumers/edit';
        $this->load->view('template/home1', $data);
    }

    function update() {
        $this->load->library('form_validation');
        $name = $this->form_validation->set_rules('name', 'Name', 'required');
        $username = $this->form_validation->set_rules('username', 'Usernmae', 'required|min_length[5]|max_length[12]|is_unique[users.username]');
        $email = $this->form_validation->set_rules('email', 'email', 'required|valid_email|is_unique[users.email]');
        $mobile_phone = $this->form_validation->set_rules('mobile_phone', 'Mobile phone', 'required');

        if ($this->form_validation->run() == FALSE) {
            $id = $this->id();
            $data['i'] = 1;
            $data['id'] = $id;
            $data['yms'] = $this->ym->admin();

            $data['book_count'] = $this->costumer->book_hitung($id);
            $data['issued_count'] = $this->costumer->issued_hitung($id);
            $data['options'] = $this->rute->get_array();
            $data['maskapai'] = $this->airline->get_array();
            $data['deposits'] = $this->deposit->index($id);

            $data['id'] = $id;
            $data['rows'] = $this->costumer->index($id);
            $data['deposits'] = $this->deposit->index($id);
            $data['content'] = 'costumers/edit';
            $this->load->view('template/home1', $data);
        } else {
            $id = $this->id();
            $name = $this->input->post('name', TRUE);
            $username = $this->input->post('username', TRUE);
            $email = $this->input->post('email', TRUE);
            $last_name = $this->input->post('last_name', TRUE);
            $address = $this->input->post('address', TRUE);
            $home_phone = $this->input->post('home_phone', TRUE);
            $mobile_phone = $this->input->post('mobile_phone', TRUE);
            $country = $this->input->post('country', TRUE);
            $city = $this->input->post('city', TRUE);
            $state = $this->input->post('state', TRUE);
            $zip_code = $this->input->post('zip_code', TRUE);
            $date = date('Y-m-d');
            $this->costumer->update($id, $name, $username, $email, $last_name, $address, $home_phone, $mobile_phone, $country, $city, $state, $zip_code, $date);
        }
    }

    function jadwal() {
        $data['id'] = $id;
        $data['expire'] = $this->costumer->get_expire($id);
        $data['content'] = 'costumers/jadwal';
        $this->load->view('template/costumer', $data);
    }

    function id() {
        $session_data = $this->session->userdata('logged_in');
        return $session_data['id'];
    }

    function flight() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $date = date('Y-m-d');
        $date_t = $this->costumer->get_token_date();

        if ($date == $date_t) {
            $data['token'] = $this->costumer->get_token();
        } else {
            $token = $this->costumer->get_token();
            $this->costumer->update_token($date, $token);
            $data['token'] = $this->costumer->get_token();
        }


        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);

        //pencarian
        if ($this->input->post()) {
            $id_maskapai = $this->input->post('id_maskapai', TRUE);
            $from = $this->input->post('from', TRUE);
            $to = $this->input->post('to', TRUE);
            $date = $this->input->post('date', TRUE);
            $pulang = $this->input->post('pulang', TRUE);
            $adult = $this->input->post('adult_post', TRUE);
            $child = $this->input->post('child_post', TRUE);
            $infant = $this->input->post('infant_post', TRUE);
            $sort = 'departureasc';
        } else {
            $array = $this->uri->segment(3);
            $url = explode('.', $array);
            $id_maskapai = $this->airline->get_idm($url[0]);
            $from = $url[1];
            $to = $url[2];
            $date = $url[3];
            $adult = $url[4];
            $child = $url[5];
            $infant = $url[6];
            $sort = $url[7];
        }

        $data['jadwals'] = $this->departure->flight($from, $to);
        //end

        $data['id_maskapai'] = $id_maskapai;
        $data['from'] = $from;
        $data['to'] = $to;
        $data['date'] = $date;
//        $data['pulang'] = $pulang;
        $data['adult_post'] = $adult;
        $data['child_post'] = $child;
        $data['infant_post'] = $infant;
        $data['sort'] = $sort;

        $data['i'] = 1;
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();

        $data['content'] = 'costumers/flight';
//        $this->load->view('template/costumer', $data);
        $this->load->view('template/home1', $data);
    }

    function flight2() {
        $id = $this->id();
        $data['id'] = $id;

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);

        //pencarian
        $id_maskapai = $this->input->post('id_maskapai', TRUE);
        $from = $this->input->post('to', TRUE);
        $to = $this->input->post('from', TRUE);
        $date = $this->input->post('date', TRUE);
        $pulang = $this->input->post('pulang', TRUE);
        $adult = $this->input->post('adult', TRUE);
        $child = $this->input->post('child', TRUE);
        $infant = $this->input->post('infant', TRUE);
        $data['jadwals'] = $this->departure->flight($from, $to, $pulang);
        //end

        $data['id_maskapai'] = $id_maskapai;
        $data['from'] = $from;
        $data['to'] = $to;
        $data['date'] = $date;
        $data['pulang'] = $pulang;
        $data['adult_post'] = $adult;
        $data['child_post'] = $child;
        $data['infant_post'] = $infant;

        $data['i'] = 1;
        $data['rows'] = $this->costumer->index($id);
        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();

        $array = array(
            'id_user' => $this->id(),
            'id_jadwal' => $this->input->post('id_jadwal', TRUE),
            'id_maskapai' => $this->input->post('id_maskapai', TRUE),
            'depart' => $this->input->post('depart', TRUE),
            'arrives' => $this->input->post('arrives', TRUE),
            'price' => $this->input->post('price', TRUE),
            'flight' => $this->input->post('flight', TRUE),
            'class' => $this->input->post('class', TRUE),
            'from' => $this->input->post('from', TRUE),
            'to' => $this->input->post('to', TRUE),
            'adult' => $this->input->post('adult', TRUE),
            'adult_price' => $this->input->post('adult_price', TRUE),
            'child' => $this->input->post('child', TRUE),
            'child_price' => $this->input->post('child_price', TRUE),
            'infant' => $this->input->post('infant', TRUE),
            'infant_price' => $this->input->post('infant_price', TRUE),
            'date' => $this->input->post('date', TRUE),
            'date_create' => date('Y-m-d H:i:s')
        );
        $data['data1'] = $array;


        $data['content'] = 'costumers/flight2';
        $this->load->view('template/home1', $data);
    }

    function booking() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;
        $data['yms'] = $this->ym->admin();

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);

        $data['infant_price'] = $this->input->post('infant_price', TRUE);

        $data['from'] = $this->input->post('from', TRUE);
        $data['to'] = $this->input->post('to', TRUE);
        $data['date'] = $this->input->post('date', TRUE);
        $data['adult_post'] = $this->input->post('adult', TRUE);
        $data['child_post'] = $this->input->post('child', TRUE);
        $data['infant_post'] = $this->input->post('infant', TRUE);
        $data['id_maskapai'] = $this->input->post('id_maskapai', TRUE);

//        date
        $ymin_c = mktime(0, 0, 0, date("m"), date("d"), date("y") - 12);
        $ymax_c = mktime(0, 0, 0, date("m"), date("d"), date("y") - 2);
        $ymin_i = mktime(0, 0, 0, date("m"), date("d"), date("y") - 2);
        $ymax_i = mktime(0, 0, 0, date("m"), date("d"), date("y"));


        $data['ymin_c'] = date("Y", $ymin_c);
        $data['ymax_c'] = date("Y", $ymax_c);
        $data['ymin_i'] = date("Y", $ymin_i);
        $data['ymax_i'] = date("Y", $ymax_i);

        $array = array(
            'id_user' => $this->id(),
            'airlines_name' => $this->input->post('airlines_name', TRUE),
            'flight_number' => $this->input->post('flight_number', TRUE),
            'price_value' => $this->input->post('price_value', TRUE),
            'price_adult' => $this->input->post('price_adult', TRUE),
            'price_child' => $this->input->post('price_child', TRUE),
            'price_infant' => $this->input->post('price_infant', TRUE),
            'simple_departure_time' => $this->input->post('simple_departure_time', TRUE),
            'simple_arrival_time' => $this->input->post('simple_arrival_time', TRUE),
            'stop' => $this->input->post('stop', TRUE),
            'duration' => $this->input->post('duration', TRUE),
            'from' => $this->input->post('from', TRUE),
            'to' => $this->input->post('to', TRUE),
            'date' => $this->input->post('date', TRUE),
//            'id_maskapai' => $this->input->post('id_maskapai', TRUE),
            'adult' => $this->input->post('adult', TRUE),
            'child' => $this->input->post('child', TRUE),
            'infant' => $this->input->post('infant', TRUE),
        );
//        $array = array(
//            'id_user' => $this->id(),
//            'id_jadwal' => $this->input->post('id_jadwal', TRUE),
//            'id_maskapai' => $this->input->post('id_maskapai', TRUE),
//            'depart' => $this->input->post('depart', TRUE),
//            'arrives' => $this->input->post('arrives', TRUE),
//            'price' => $this->input->post('price', TRUE),
//            'flight' => $this->input->post('flight', TRUE),
//            'class' => $this->input->post('class', TRUE),
//            'from' => $this->input->post('from', TRUE),
//            'to' => $this->input->post('to', TRUE),
//            'adult' => $this->input->post('adult', TRUE),
//            'adult_price' => $this->input->post('adult_price', TRUE),
//            'child' => $this->input->post('child', TRUE),
//            'child_price' => $this->input->post('child_price', TRUE),
//            'infant' => $this->input->post('infant', TRUE),
//            'infant_price' => $this->input->post('infant_price', TRUE),
//            'date' => $this->input->post('date', TRUE),
//            'date_create' => date('Y-m-d H:i:s')
//        );

        $data['i'] = 1;
        if ($this->input->post('data1', TRUE)) {
            $data['detail'] = $this->input->post('data1', TRUE);
            $data['detail2'] = $array;
            $detail = $this->input->post('data1', TRUE);
        } else {
            $data['detail'] = $array;
            $data['detail2'] = "";
        }


        $data['deposits'] = $this->deposit->index($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();

        $data['content'] = 'costumers/booking';
        $this->load->view('template/home1', $data);

        $this->costumer->booking_temp($id, $data);
    }

    function booking_create() {

        $booking1 = $this->input->post('booking1', TRUE);
        $booking2 = $this->input->post('booking2', TRUE);
        $syarat = $this->input->post('syarat', TRUE);
        $id_jadwal = $this->input->post('id_jadwal', TRUE);
        $id_jadwal2 = $this->input->post('id_jadwal2', TRUE);

//        var_dump($booking1);  


        $qty = $this->input->post('qty', TRUE);

        $code = $this->input->post('code', TRUE);
        $data1 = array(
//            'price' => $this->input->post('total1', TRUE),
            'code' => $this->input->post('code', TRUE),
            'title_dk' => $this->input->post('title_dk', TRUE),
            'nama_d_dk' => $this->input->post('nama_d_dk', TRUE),
            'nama_b_dk' => $this->input->post('nama_b_dk', TRUE),
            'kode_negara_dk' => $this->input->post('kode_negara_dk', TRUE),
            'kode_wilayah_dk' => $this->input->post('kode_wilayah_dk', TRUE),
            'nomor_telp_dk' => $this->input->post('nomor_telp_dk', TRUE),
            'kode_negara_s_dk' => $this->input->post('kode_negara_s_dk', TRUE),
            'no_telp_selular_dk' => $this->input->post('no_telp_selular_dk', TRUE),
            'catatan_dk' => $this->input->post('catatan_dk', TRUE),
            'date_create' => date('Y-m-d H:i:s'),
            'expire' => date("Y-m-d H:i:s", mktime(date("H") + 2, date("i"), date("s"), date("m"), date("d"), date("Y"))),
        );

        $adult = $this->input->post('adult', TRUE);
        $child = $this->input->post('child', TRUE);
        $infant = $this->input->post('infant', TRUE);
        $save1 = array_merge($booking1, $data1);
//        $this->departure->update_qtty($id_jadwal, $qty);
        $this->costumer->booking_create($save1);

        foreach ($adult as $val) {
            $this->costumer->booking_create_adult($val);
        }
        if (!$child == null) {
            foreach ($child as $val) {
                $this->costumer->booking_create_child($val);
            }
        }
        if (!$infant == null) {
            foreach ($infant as $val) {
                $this->costumer->booking_create_infant($val);
            }
        }
//        
//        $this->test($id);

        $this->email_alert_book($code);

        $notif = "sukses";
        redirect('costumers/status_issued/' . $notif);
    }

    function email_alert_book($code) {

        $id = $this->id();
        $data['id'] = $id;
        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['id'] = $id;
        $data['rows'] = $this->costumer->show_book($code, $id);
        $data['deposits'] = $this->deposit->index($id);
        $email = $this->email->email_array();
        $email_user = $this->costumer->get_email($id);
        $email_user = array($email_user);
//        $this->load->view('costumers/email_book', $data);
//        $config['mailtype'] = 'html';
//        $this->email->initialize($config);
        $message = $this->load->view('costumers/email_book', $data, TRUE);
//        $this->load->view('costumers/email_book', $data);
//        
        $this->load->library('email');
        $this->email->set_mailtype("html");
        $this->email->from('stasiuntiketonline.com');

        //email
        $list = array_merge($email, $email_user);
        $this->email->to($list);
        $this->email->cc('');
        $this->email->subject('Booking');
        $this->email->message($message);
        $this->email->send();
        return true;
    }

    function confirm_booking() {
        $id_costumer = $this->id();
        $id = $this->input->post('id');
        $deposit = $this->input->post('deposit');
        $price = $this->input->post('total_price');
        $code = $this->input->post('code');
        $keterangan = $this->input->post('keterangan');

        $url = base_url() . 'deposits/add';
        if ($price > $deposit) {
            echo ("<SCRIPT LANGUAGE='JavaScript'>
                window.alert('Total Deposit Anda $deposit kurang dari Total harga $price silahkan melakukan deposit')
                window.location.href='$url';
                </SCRIPT>");
        } else {
            $tgl = date('Y-m-d');
            $jam = date('H:i');
            $date_create = date('Y-m-d');
            $this->deposit->pay($id_costumer, $price, $tgl, $jam, $keterangan, $date_create);
            $this->booking->data_benar($id);
            $this->email_issued($code);

            redirect('costumers/show_book/' . $id . '.confirm');
        }
    }

    function email_issued($code) {
        $id = $this->id();
        $data['id'] = $id;
        $data['i'] = 1;
        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);
        $data['options'] = $this->rute->get_array();
        $data['maskapai'] = $this->airline->get_array();
        $data['deposits'] = $this->deposit->index($id);

        $data['id'] = $id;
        $data['rows'] = $this->costumer->show_book($code, $id);
        $data['deposits'] = $this->deposit->index($id);
        $email = $this->email->email_array();
        $email_user = $this->costumer->get_email($id);
        $email_user = array($email_user);
        $message = $this->load->view('costumers/email_issued', $data, TRUE);
        $this->load->library('email');
        $this->email->set_mailtype("html");
        $this->email->from('stasiuntiketonline.com');

        //email
        
        $list = array_merge($email, $email_user);
        $this->email->to($list);
        $this->email->cc('');
        $this->email->subject('Issued tiket');
        $this->email->message($message);
        $this->email->send();
        return true;
    }

    function issued() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $data['id'] = $id;

        $data['book_count'] = $this->costumer->book_hitung($id);
        $data['issued_count'] = $this->costumer->issued_hitung($id);

        $data['content'] = 'costumers/issued';
        $this->load->view('template/costumer', $data);
    }

    function confirmissue() {
        $id_costumer = $this->id();
        $id_book = $this->input->post('id', TRUE);
        $total = $this->input->post('total', TRUE);
        $this->costumer->confirmbook($id_book, $id_costumer, $total);
        $this->costumer->confirmissue($id_costumer, $total);
    }

    function download() {
        $url = $this->uri->segment(3);
//        $url = 'cokelat_bendera_a';
//        $name = get_download($url);
        $file = get_file($url);
        $data = file_get_contents("tiket/" . $file);
        $name = $file;
        force_download($name, $data);
    }

    function file() {
        $data['file'] = $this->uri->segment(3);
        $this->load->view('page/file', $data);
    }

    function test_a() {
        $id = $this->id();
        $data['expire'] = $this->costumer->get_expire($id);
        $email = $this->email->email_array();
        $email_user = $this->costumer->get_email($id);
        $email_user = array($email_user);
        $email = array_merge($email, $email_user);
        print_r($email);
        
//        for($i = 0; $i <= 1; $i++){
//            $echo = $email[$i]['email'];
//        }
//        echo $email;
                
//        $this->load->view('page/test');
    }

    function index_test() {
        $this->load->helper(array('form', 'url'));

        $this->load->library('form_validation');

        $this->form_validation->set_rules('username', 'Username', 'callback_username_check');
        $this->form_validation->set_rules('password', 'Password', 'required');
        $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
        $this->form_validation->set_rules('email', 'Email', 'required|is_unique[users.email]');


        if ($this->form_validation->run() == FALSE) {
            $this->load->view('page/test');
        } else {
            $this->load->view('formsuccess');
        }
    }

    public function username_check($str) {
        if ($str == 'test') {
            $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
            return FALSE;
        } else {
            return TRUE;
        }
    }

}

?>
